package com.youhome.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.mapper.BSendRecordMailMapper;
import com.youhome.service.model.BSendRecordMail;
import com.youhome.service.model.BUser;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BSendRecordMailService;
import com.youhome.service.service.BUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by chen365 on 2017/12/20.
 */
@Service
public class BSendRecordMailServiceImpl implements BSendRecordMailService {
    @Autowired
    private BSendRecordMailMapper bSendRecordMailMapper;

    @Override
    public BSendRecordMail selectByPrimaryKey(Long id) {
        return bSendRecordMailMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<BSendRecordMail> selectByBean(Integer pageIndex, Integer pageSize, BSendRecordMail record) {
        if (StringUtils.isEmpty(pageIndex) || pageIndex < 1) {
            pageIndex = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        List<BSendRecordMail> list = bSendRecordMailMapper.selectByBean(record);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<BSendRecordMail> selectByBean(Principal principal, Long gatherId, Integer pageNumber, Integer pageSize) throws CustomException {
        if (StringUtils.isEmpty(gatherId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }

        Long userId = principal.getUser().getUserId();

        BSendRecordMail record = new BSendRecordMail();
        record.setUserId(userId);
        record.setGatherId(gatherId);

        if (StringUtils.isEmpty(pageNumber) || pageNumber < 1) {
            pageNumber = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<BSendRecordMail> list = bSendRecordMailMapper.selectByBean(record);
        return new PageInfo<>(list);

    }
}
